// CoordinateTransformStrategy.h
#pragma once
#include <QString>
#include "MotorConfig.h"


class CoordinateTransformStrategy {
public:
    virtual ~CoordinateTransformStrategy() = default;

    // 将内部坐标转换为外部坐标（显示给用户）
    virtual double internalToExternal(double internalPosition) const = 0;

    // 将外部坐标转换为内部坐标（发送给电机）
    virtual double externalToInternal(double externalPosition, bool isRelative) const = 0;

    // 验证外部坐标是否有效
    virtual bool isValidExternalPosition(double externalPosition, bool isRelative) const = 0;

    // 获取无效位置的错误消息
    virtual QString getInvalidPositionMessage(bool isRelative) const = 0;

    // 设置正向/反向模式
    virtual void setPositiveMode(bool isPositiveMode) = 0;

    // 获取当前模式
    virtual bool isPositiveMode() const = 0;

    // 获取最大位置
    virtual double getMaxPosition() const { return MaxPosition; }

    virtual void setMaxPosition(double maxPosition) {
        MaxPosition = maxPosition;
    }

    virtual double getInitPosition() const { return InitPosition; }

    virtual void setInitPosition(double initPosition) {
        InitPosition = initPosition;
    }

    double MaxPosition=0;
    double InitPosition=0;
};

class DefaultMotorStrategy : public CoordinateTransformStrategy {
public:
    DefaultMotorStrategy() {}

    double internalToExternal(double internalPosition) const override {
        // 默认策略：内部坐标和外部坐标相同
        return internalPosition;
    }

    double externalToInternal(double externalPosition, bool isRelative) const override {
        // 默认策略：外部坐标和内部坐标相同
        return externalPosition;
    }

    bool isValidExternalPosition(double externalPosition, bool isRelative) const override {
        // 默认策略：所有位置都有效
        return true;
    }

    QString getInvalidPositionMessage(bool isRelative) const override {
        return "位置无效";
    }

    void setPositiveMode(bool isPositiveMode) override {
        // 默认策略不需要处理模式
    }

    bool isPositiveMode() const override {
        // 默认返回true
        return true;
    }
};


class FuYuMotorStrategy : public CoordinateTransformStrategy {
public:
    FuYuMotorStrategy(bool isPositiveMode = false, 
                     double maxPosition = 0,
                     double initPosition = 0)
        : m_isPositiveMode(isPositiveMode), m_maxPosition(maxPosition), m_initPosition(initPosition) {
        MotorConfig* config = MotorConfig::getInstance();
        if (config) {
            m_maxPosition = config->getMotorMaxTravel();
            m_initPosition = config->getOpticalPathMin();
        }
    }

    //电机坐标转用户视角光斑坐标
    double internalToExternal(double internalPosition) const override {
        if (m_isPositiveMode) {
             return  m_initPosition+2*m_maxPosition+internalPosition;
        }
        return internalPosition*2+m_initPosition;;
    }

    double externalToInternal(double externalPosition, bool isRelative) const override {
        if (!isRelative) {
            if (m_isPositiveMode)
                return (externalPosition-m_initPosition)/2-m_maxPosition;
            return (externalPosition-m_initPosition)/2;
        }
                return externalPosition/2;

    }

    bool isValidExternalPosition(double externalPosition, bool isRelative) const override {
        if (isRelative) {
            return true;
        }
        return externalPosition >= m_initPosition && externalPosition <= m_initPosition+m_maxPosition*2;
    }

    QString getInvalidPositionMessage(bool isRelative) const override {
        if (isRelative) {
            return "相对位移无效";
        }
      return QString("位置超出范围%0~%1").arg(m_initPosition).arg(m_initPosition+m_maxPosition*2);
    }

    void setMaxPosition(double maxPosition) {
        m_maxPosition = maxPosition;
    }

    double getMaxPosition() const override {
        return m_initPosition+m_maxPosition*2;
    }
    void setPositiveMode(bool isPositiveMode) override {
        m_isPositiveMode = isPositiveMode;
    }

    bool isPositiveMode() const override {
        return m_isPositiveMode;
    }
    void setInitPosition(double initPosition) {
        m_initPosition = initPosition;
    }

    double getInitPosition() const override {
        return m_initPosition;
    }

private:
    bool m_isPositiveMode=false;
    double m_maxPosition=0;;
    double m_initPosition=0;;
};